package com.ara.copy.presenter;


import com.ara.copy.contract.CopyContract;
import com.ara.copy.model.CopyModel;
import com.ara.common.kernel.base.BasePresenter;
import com.ara.common.kernel.bean.BaseBean;
import com.ara.common.kernel.network.exception.DataException;
import com.ara.common.kernel.network.exception.ExceptionHandle;
import com.ara.common.kernel.network.util.RxSubscriberUtils;

/**
 * Created by XieXin on 2021/5/18.
 * 复制 Presenter层
 */
public class CopyPresenter extends BasePresenter<CopyContract.View> implements CopyContract.Presenter {
    private CopyModel model;

    public CopyPresenter(CopyContract.View view) {
        super(view);
        model = new CopyModel();
    }


    @Override
    public void copy() {
        if (!isBind()) return;//没有绑定停止执行
        if (isNotNet()) return;
        getView().showLoadingProgress();
        compositeDisposable.add(model.copy()
                .subscribeWith(new RxSubscriberUtils<Object>() {
                    @Override
                    protected void doOnNext(BaseBean<Object> bean) {
                        if (bean.getData() != null) {
                            getView().dismissLoadingProgress();
                            getView().onCopySuccess();
                        } else {
                            doOnError(DataException.create());
                        }
                    }

                    @Override
                    protected void doOnError(ExceptionHandle.ResponseThrowable e) {
                        getView().dismissLoadingProgress();
//                        getView().onToast(e);
                        getView().onError(0, e);
                    }
                }));
    }

}
